# Build script for qi.
#
# Copyright (c) 2015-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

echo "Making Qi from ${worktree}/qi ..."

cd -- "${worktree}/qi"

make -j1 clean

./configure \
 --prefix=/tools \
 --packagedir=/usr/pkg \
 --targetdir=/ \
 --arch=$package_arch

make -j1 install

# Clean up the source repository

rm -f config.mak
make -j1 clean

# Make "usr/pkg" directory and output
# directory for packages available
mkdir -p -- "${rootdir}/usr/pkg" "${rootdir}/var/cache/qi/packages"

# Adjust prefixes in config for final system installation
sed -i \
 -e 's|/tools|/usr|g'   \
 -e 's|/usr/etc|/etc|'  \
 -e 's|/usr/var|/var|'  \
 /tools/etc/qirc

# Set default compiler options for Dragora
case $arch in
i586)
    sed -i \
     -e 's|#QICFLAGS=.*|QICFLAGS=\"-O2 -march=pentium-mmx -mtune=pentiumpro -fstack-clash-protection -fcf-protection=full -fstack-protector-strong --param=ssp-buffer-size=4 -pipe\"|' \
     -e 's|#QICXXFLAGS=.*|QICXXFLAGS=\"-O2 -march=pentium-mmx -mtune=pentiumpro -fstack-clash-protection -fcf-protection=full -fstack-protector-strong --param=ssp-buffer-size=4 -pipe\"|' \
     -e 's|#QILDFLAGS=.*|QILDFLAGS=\"-s\"|' \
     -e 's|#QICPPFLAGS=.*|QICPPFLAGS=\"-D_FORTIFY_SOURCE=2\"|' \
     /tools/etc/qirc
    ;;
x86_64)
    sed -i \
     -e 's|#QICFLAGS=.*|QICFLAGS=\"-O2 -march=x86-64 -fstack-clash-protection -fcf-protection=full -fstack-protector-strong --param=ssp-buffer-size=4 -pipe\"|' \
     -e 's|#QICXXFLAGS=.*|QICXXFLAGS=\"-O2 -march=x86-64 -fstack-clash-protection -fcf-protection=full -fstack-protector-strong --param=ssp-buffer-size=4 -pipe\"|' \
     -e 's|#QILDFLAGS=.*|QILDFLAGS=\"-s\"|' \
     -e 's|#QICPPFLAGS=.*|QICPPFLAGS=\"-D_FORTIFY_SOURCE=2\"|' \
     /tools/etc/qirc
    ;;
*)
    sed -i \
     -e 's|#QICFLAGS=.*|QICFLAGS=\"-O2 -fstack-protector-strong --param=ssp-buffer-size=4 -pipe\"|' \
     -e 's|#QICXXFLAGS=.*|QICXXFLAGS=\"-O2 -fstack-protector-strong --param=ssp-buffer-size=4 -pipe\"|' \
     -e 's|#QILDFLAGS=.*|QILDFLAGS=\"-s\"|' \
     -e 's|#QICPPFLAGS=.*|QICPPFLAGS=\"-D_FORTIFY_SOURCE=2\"|' \
     /tools/etc/qirc
    ;;
esac

# Include additional settings from targets/

# We prefix '--enable-multilib' for targets supporting it,
# the native GCC wants this option explicitly
if test -n "$multilib_options"
then
    case $multilib_options in
    *--disable-multilib* | *--enable-multilib* | *--with-multilib-list=)
        true
        ;;
    *)
        multilib_options="--enable-multilib $multilib_options"
        ;;
    esac
fi

cat << EOF >> /tools/etc/qirc

# Multilib options
multilib_options="$multilib_options"

# lib<equal> suffix (multilib, if any)
libSuffix="$libSuffix"

# GCC (arch-specific) options
gcc_options="$gcc_options"
EOF

chmod 644 /tools/etc/qirc

